package com.wf.controller;

import com.github.pagehelper.PageInfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wf.entity.Userinfo;
import com.wf.service.UserinfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * Created by yuzhongbo on 2017/5/3.
 */
@RestController
@RequestMapping(value="/userinfo")
public class UserinfoController {
    private final Logger logger=Logger.getLogger(UserinfoController.class);

    @Autowired
    private UserinfoService userinfoService;

    @HystrixCommand
    @RequestMapping(value="/getUserList/{name}", method=RequestMethod.GET)
    public List<Userinfo> getUserList(@PathVariable(value = "name") String name){
        logger.info("===call getUserListP===");
        Userinfo userinfo=new Userinfo();
        userinfo.setUsername(name);
        List<Userinfo> userinfos = userinfoService.select(userinfo);
        return userinfos;
    }
    @RequestMapping(value="/getUserListTwo", method=RequestMethod.POST)
    public PageInfo<Userinfo> getUserListTwo(@ModelAttribute(value = "userinfo") Userinfo userinfo) {
        logger.info("===call getUserListPT===");
        List<Userinfo> userinfoList = userinfoService.getAllByPage(userinfo);
        PageInfo<Userinfo> userinfopages=new PageInfo<Userinfo>(userinfoList);
        return userinfopages;
    }

    @ApiOperation(value="获取用户列表", notes="根据Userinfo获取用户列表")
    @ApiImplicitParam(name = "userinfo", value = "Userinfo", required = false, dataType = "Userinfo")
    @RequestMapping
    @CrossOrigin
    public PageInfo<Userinfo> getAll(Userinfo userinfo) {
    List<Userinfo> userinfoList = userinfoService.getAllByPage(userinfo);
    return new PageInfo<Userinfo>(userinfoList);
    }
    @RequestMapping(value = "/add")
    public Userinfo add() {
        return new Userinfo();
    }

    @RequestMapping(value = "/view/{id}")
    @CrossOrigin
    public Userinfo view(@PathVariable Integer id) {
        ModelAndView result = new ModelAndView();
        Userinfo userinfo = userinfoService.getById(id);
        return userinfo;
    }

    @RequestMapping(value = "/delete/{id}")
    @CrossOrigin
    public ModelMap delete(@PathVariable Integer id) {
        ModelMap result = new ModelMap();
        userinfoService.deleteById(id);
        result.put("msg", "删除成功!");
        return result;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelMap save(Userinfo userinfo) {
        ModelMap result = new ModelMap();
        String msg = userinfo.getId() == null ? "新增成功!" : "更新成功!";
        userinfoService.save(userinfo);
        result.put("userinfo", userinfo);
        result.put("msg", msg);
        return result;
    }


}
